import config from "./config.js";

class api {
	constructor() {
		this.BaseUrl = config.ajaxUrl.BaseUrl
	}

	/**
	 * @param {Object} params
	 * url: 请求地址
	 * data {Object}
	 */
	post(params) {
		var token = "";
		try {
			const value = uni.getStorageSync('token');
			if (value) {
				token = value
			}
		} catch (e) {
			console.log("token 不存在")
		}

		return new Promise((resolve, reject) => {
			uni.request({
				url: `${this.BaseUrl}${params.url}`,
				method: 'POST',
				data: params.data,
				header: {
					'token': token
				},
				success: (res) => {

					resolve(res.data)
					// resolve(this.ValidationData(res.data))
				}
			});
		})
	}

	ValidationData(result) {
		switch (result.status){
			case "200":
				return result.data
				break;
			case "500":
				uni.showToast({
					title: result.message,
					icon: 'none',
					duration: 2000
				});
				return null
				break;
		}
	}
}

export default new api()